package com.sgb.zhaocai.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.ProjectIssueListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.view.demand.activity.ProjectIssueListActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Time:2021/9/1
 * AUTHOR:F_Q
 * Description:
 */
public class ProjectIssueListModel extends ViewModel {

    private boolean isLastPage;

    /**
     * 获取项目管理列表
     * @param activity
     * @param requestBean
     */
    public void ProjectIssueList(ProjectIssueListActivity activity, RequestBean requestBean) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getProjectIssueList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<ProjectIssueListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<ProjectIssueListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                    activity.setProjectIssueList(data.getData().getList(),isLastPage);
                }else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }

    /**
     * 去详情界面
     * @param activity
     * @param id
     */
    public void getWebH5(ProjectIssueListActivity activity, String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUrlSetting("FProjectDetails").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    String webUrl = data.getData().getUrl() + "?id=" + id;
                    BaseApi.startWeb(activity, webUrl);
                }else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }

    /**
     * 经营帮的数据是否公示
     * @param activity
     * @param announce
     * @param id
     */
    public void getIsPublicity(ProjectIssueListActivity activity, String announce, String id) {
        Map map = new HashMap();
        if (TextUtils.equals(announce,"1")){
            map.put("announce","0");
        }else {
            map.put("announce","1");
        }
        map.put("projectId",id);
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUpdateProjectIsAnnounce(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    activity.setIsPublicity();
                }else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }
}
